{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9faa4cd2",
   "metadata": {},
   "source": [
    "# 1.python 使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b5ebb235",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "10c0110f",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "API_key = '5E5V4cXjdk_y_Fnoa2gIo6Rn2qj5NYjz'\n",
    "API_sercret = \"YlXvJybg0abyNg5bBfISXxjgg417dfhZ\"\n",
    "\n",
    "# 3. 准备base url （face detect）\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 请求方法： POST\n",
    "\n",
    "# 5. 准备请求参数： payload\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.doubanio.com%2Fview%2Fgroup_topic%2Fl%2Fpublic%2Fp451078141.jpg&refer=http%3A%2F%2Fimg2.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651203070&t=4b3ee3fb72e3d33f48b3fcfdf3481203'    \n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "# 6, 请求数据\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "79a7ad2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "11dd163a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648611091,e17f116f-f242-461b-bab0-21636b6c42cc\",\"time_used\":357,\"faces\":[{\"face_token\":\"c63fc1b738a7c81c0ba42332efe7bae2\",\"face_rectangle\":{\"top\":417,\"left\":325,\"width\":425,\"height\":425}}],\"image_id\":\"Y9VJ8cGSRXRao1POyIXydQ==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0f577192",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c63fc1b738a7c81c0ba42332efe7bae2'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]['face_token']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "77028628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c63fc1b738a7c81c0ba42332efe7bae2'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a9655f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b0109321",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '5E5V4cXjdk_y_Fnoa2gIo6Rn2qj5NYjz'\n",
    "API_sercret = 'YlXvJybg0abyNg5bBfISXxjgg417dfhZ'\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes':'facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = analyze_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "77515489",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5c7de9a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 167,\n",
       " 'request_id': '1648611252,a6148582-570d-45ab-8a4a-f46bc57940ac',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 7.269,\n",
       "     'neutral': 90.774,\n",
       "     'disgust': 0.031,\n",
       "     'anger': 0.031,\n",
       "     'surprise': 1.708,\n",
       "     'fear': 0.039,\n",
       "     'happiness': 0.148},\n",
       "    'beauty': {'female_score': 81.116, 'male_score': 82.532},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 99.999,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 0.001,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 99.997,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 0.003,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'glass': {'value': 'Normal'},\n",
       "    'skinstatus': {'dark_circle': 6.467,\n",
       "     'stain': 37.172,\n",
       "     'acne': 37.894,\n",
       "     'health': 10.365},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.378},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.378},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.378}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 92.83},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.469,\n",
       "      'vector_z_component': 0.982,\n",
       "      'vector_x_component': -0.012,\n",
       "      'vector_y_component': -0.19,\n",
       "      'position_y_coordinate': 0.395},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.482,\n",
       "      'vector_z_component': 0.991,\n",
       "      'vector_x_component': -0.016,\n",
       "      'vector_y_component': -0.136,\n",
       "      'position_y_coordinate': 0.375}}},\n",
       "   'face_rectangle': {'width': 425, 'top': 417, 'left': 325, 'height': 425},\n",
       "   'face_token': 'c63fc1b738a7c81c0ba42332efe7bae2'}]}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "456b6763",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1a39f75",
   "metadata": {},
   "source": [
    "# 1、数据处理小魔法 pandas.json_normalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "33f6717f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>c63fc1b738a7c81c0ba42332efe7bae2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>7.269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>90.774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>1.708</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.039</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>0.148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>81.116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>82.532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>99.999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>99.997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>0.003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>6.467</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>37.172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>37.894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>10.365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>92.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>425</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          c63fc1b738a7c81c0ba42332efe7bae2\n",
       "attributes.emotion.sadness                                                     7.269\n",
       "attributes.emotion.neutral                                                    90.774\n",
       "attributes.emotion.disgust                                                     0.031\n",
       "attributes.emotion.anger                                                       0.031\n",
       "attributes.emotion.surprise                                                    1.708\n",
       "attributes.emotion.fear                                                        0.039\n",
       "attributes.emotion.happiness                                                   0.148\n",
       "attributes.beauty.female_score                                                81.116\n",
       "attributes.beauty.male_score                                                  82.532\n",
       "attributes.mouthstatus.close                                                     0.0\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    100.0\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                            99.999\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                             0.001\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                            99.997\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                  0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             0.003\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.glass.value                                                        Normal\n",
       "attributes.skinstatus.dark_circle                                              6.467\n",
       "attributes.skinstatus.stain                                                   37.172\n",
       "attributes.skinstatus.acne                                                    37.894\n",
       "attributes.skinstatus.health                                                  10.365\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.378\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.378\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.378\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                   92.83\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.469\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.982\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.012\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                             -0.19\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.395\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.482\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.991\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                            -0.016\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                            -0.136\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.375\n",
       "face_rectangle.width                                                             425\n",
       "face_rectangle.top                                                               417\n",
       "face_rectangle.left                                                              325\n",
       "face_rectangle.height                                                            425"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a99d666",
   "metadata": {},
   "source": [
    "# 3、人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "cb9a8b80",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "API_key = '5E5V4cXjdk_y_Fnoa2gIo6Rn2qj5NYjz'\n",
    "API_sercret = \"YlXvJybg0abyNg5bBfISXxjgg417dfhZ\"\n",
    "\n",
    "# 3. 准备base url （face detect）\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 请求方法： POST\n",
    "\n",
    "# 5. 准备请求参数： payload\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.doubanio.com%2Fview%2Fgroup_topic%2Fl%2Fpublic%2Fp451078141.jpg&refer=http%3A%2F%2Fimg2.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651203070&t=4b3ee3fb72e3d33f48b3fcfdf3481203'    \n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "# 6, 请求数据\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "a3810ec3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'321af710fe766af1d3a4ac7f7a62bf44'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f85bbea5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '5E5V4cXjdk_y_Fnoa2gIo6Rn2qj5NYjz'\n",
    "API_sercret = 'YlXvJybg0abyNg5bBfISXxjgg417dfhZ'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2F50%2Fv2-1ef203a5ad44ba21c4a6d3232c992477_hd.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651203588&t=1664a82fc80547266f8cd797b1354b3c'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "0cfbca3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'41c880385001d18093194c662d777c69'"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcb47215",
   "metadata": {},
   "source": [
    "## 2、人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "06602482",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '5E5V4cXjdk_y_Fnoa2gIo6Rn2qj5NYjz'\n",
    "API_sercret = 'YlXvJybg0abyNg5bBfISXxjgg417dfhZ'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "8393c6e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "2cb3f5b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648611750,6e64181f-df14-4c5a-a2ab-4bc48d09e730',\n",
       " 'time_used': 337,\n",
       " 'confidence': 63.216,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0352449",
   "metadata": {},
   "source": [
    "# 4. 封装人脸检测函数，实现快速调用\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "1001f1d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = '5E5V4cXjdk_y_Fnoa2gIo6Rn2qj5NYjz'\n",
    "API_sercret = 'YlXvJybg0abyNg5bBfISXxjgg417dfhZ'\n",
    "img_url_01 = 'https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E6%9D%8E%E9%92%9F%E7%A1%95%E8%AF%81%E4%BB%B6%E7%85%A7&step_word=&hs=0&pn=0&spn=0&di=7060663421280190465&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=4225362094%2C187496513&os=3043058668%2C2609204824&simid=4225362094%2C187496513&adpicid=0&lpn=0&ln=1588&fr=&fmq=1648611580715_R&fm=result&ic=&s=undefined&hd=&latest=&copyright=&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fpic1.zhimg.com%2F50%2Fv2-1ef203a5ad44ba21c4a6d3232c992477_hd.jpg%26refer%3Dhttp%3A%2F%2Fpic1.zhimg.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1651203588%26t%3D1664a82fc80547266f8cd797b1354b3c&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bziti7_z%26e3Bv54AzdH3Fq7jfpt5gAzdH3Fn0lbcblbn&gsm=1&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined&dyTabStr=MCwzLDIsNiwxLDQsNSw4LDcsOQ%3D%3D'\n",
    "img_url_02 = 'https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E7%BD%97%E4%B8%80%E8%88%9F%E8%AF%81%E4%BB%B6%E7%85%A7&step_word=&hs=0&pn=8&spn=0&di=7060663421343105025&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=4033875977%2C911130627&os=1053386220%2C735351784&simid=4033875977%2C911130627&adpicid=0&lpn=0&ln=171&fr=&fmq=1648611062504_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined&copyright=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=15&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fimg2.doubanio.com%2Fview%2Fgroup_topic%2Fl%2Fpublic%2Fp451078141.jpg%26refer%3Dhttp%3A%2F%2Fimg2.doubanio.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1651203070%26t%3D4b3ee3fb72e3d33f48b3fcfdf3481203&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B157kwg_z%26e3Bv54AzdH3F2657rAzdH3Fp5rtvAzdH3FddbcccmnbAzdH3F%3Fpyrj%3D6jv&gsm=9&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined&dyTabStr=MCwzLDIsNiwxLDQsNSw4LDcsOQ%3D%3D'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "144a93de",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "8b8afc0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648612075,c828cb03-34d2-467c-83b6-6f586d0b418e',\n",
       " 'time_used': 243,\n",
       " 'error_message': 'IMAGE_ERROR_UNSUPPORTED_FORMAT: image_url'}"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "3997e666",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648612083,0d0d5fe3-d4cf-4ebb-b6bf-91cfddbda024',\n",
       " 'time_used': 162,\n",
       " 'error_message': 'IMAGE_ERROR_UNSUPPORTED_FORMAT: image_url'}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc262243",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
